package com.snake.mylibrary.db_provider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;


public class ContentResolverManager {

    private ContentResolver mResolver;
    public ContentResolverManager(Context context, Uri uri) {
        init(context,uri);
    }
    private void init(Context context, Uri uri) {
        mResolver = context.getContentResolver();
        mResolver.registerContentObserver(uri, true, new DBContentObserver(new Handler()));
    }

    public void insert(Uri uri ,ContentValues values){
        mResolver.insert(uri,values);
    }

    public void delete(Uri uri ,String where,String[] selectionArgs){
        mResolver.delete(uri,where,selectionArgs);
    }

    public void update(Uri uri ,ContentValues values,String where,String[] selectionArgs){
        mResolver.update(uri,values,where,selectionArgs);
    }

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){
        Cursor cursor = mResolver.query(uri, projection, selection, selectionArgs, sortOrder);
        return cursor;
    }


    class DBContentObserver extends ContentObserver {

        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public DBContentObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            Log.e("DBContentObserver", "onChange: 数据发生改变了");
        }
    }
}
